/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.sanerzone.smscenter.modules.sms.web;

import com.sanerzone.jeebase.common.config.Global;
import com.sanerzone.jeebase.common.persistence.Page;
import com.sanerzone.jeebase.common.utils.StringUtils;
import com.sanerzone.jeebase.common.web.BaseController;
import com.sanerzone.smscenter.biz.iface.CachRefreshInterface;
import com.sanerzone.smscenter.modules.sms.entity.SmsGateway;
import com.sanerzone.smscenter.modules.sms.service.SmsGatewayService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 网关信息Controller
 *
 * @author xql
 * @version 2018-04-24
 */
@Controller
@RequestMapping(value = "${adminPath}/sms/smsGateway")
public class SmsGatewayController extends BaseController {
    @Autowired
    private CachRefreshInterface cachRefreshInterface;

    @Autowired
    private SmsGatewayService smsGatewayService;

    @ModelAttribute
    public SmsGateway get(@RequestParam(required = false) String id) {
        SmsGateway entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = smsGatewayService.get(id);
        }
        if (entity == null) {
            entity = new SmsGateway();
        }
        return entity;
    }

    @RequiresPermissions("sms:smsGateway:view")
    @RequestMapping(value = {"list", ""})
    public String list(SmsGateway smsGateway, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<SmsGateway> page = smsGatewayService.findPage(new Page<SmsGateway>(request, response), smsGateway);
        model.addAttribute("page", page);
        return "modules/sms/smsGatewayList";
    }

    @RequiresPermissions("sms:smsGateway:view")
    @RequestMapping(value = "test")
    public String test(SmsGateway smsGateway, HttpServletRequest request, HttpServletResponse response, Model model) {
//		Page<SmsGateway> page = smsGatewayService.findPage(new Page<SmsGateway>(request, response), smsGateway);
//		model.addAttribute("page", page);
        model.addAttribute("smsGateway", smsGateway);
        return "modules/sms/gatewayTest";
    }

    @RequiresPermissions("sms:smsGateway:view")
    @RequestMapping(value = "form")
    public String form(SmsGateway smsGateway, Model model) {
        model.addAttribute("smsGateway", smsGateway);
        return "modules/sms/smsGatewayForm";
    }

    @RequiresPermissions("sms:smsGateway:edit")
    @RequestMapping(value = "save")
    public String save(SmsGateway smsGateway, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, smsGateway)) {
            return form(smsGateway, model);
        }
        SmsGateway smsGatewayDB =smsGatewayService.get(smsGateway.getId());
        smsGatewayService.save(smsGateway);
        if (smsGateway.getGwStatus() == 0 || smsGateway.getGwStatus() == -1) {
            smsGateway.setGwReceiveModel("0");
            smsGateway.setGwDeliverStatus("0");
        }
        boolean res = smsGatewayService.remoteSaveGateway(smsGateway, smsGatewayDB);
        addMessage(redirectAttributes, "通道保存成功");
        return "redirect:" + Global.getAdminPath() + "/sms/smsGateway/?repage";
    }

    @RequiresPermissions("sms:smsGateway:edit")
    @RequestMapping(value = "delete")
    public String delete(SmsGateway smsGateway, RedirectAttributes redirectAttributes) {
        smsGatewayService.delete(smsGateway);
        addMessage(redirectAttributes, "删除网关信息成功");
        return "redirect:" + Global.getAdminPath() + "/sms/smsGateway/?repage";
    }

}